﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
namespace BeatEmUp
{
    public class ResolutionManager
    {
        GameManager game;
        int currentX, currentY;
        double ratioX, ratioY;
        int standardResolutionX = 0, standardResolutionY = 0;

        public ResolutionManager(GameManager game)
        {
            this.game = game;
        }

        /// <summary>
        /// Resolution manager constructor
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="StandardGameResolution">Type here the standard resolution that the game is optimized to</param>
        public ResolutionManager(GameManager game, Vector2 StandardGameResolution)
        {
            this.game = game;
            standardResolutionX = (int)StandardGameResolution.X;
            standardResolutionY = (int)StandardGameResolution.Y;
            this.ChangeResolutionTo(standardResolutionX, standardResolutionY);
        }

        internal void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle sourceRectangle,Color color)
        {
            currentX = game.graphics.PreferredBackBufferWidth;
            currentY = game.graphics.PreferredBackBufferHeight;
            ratioX = (double)currentX / (double)standardResolutionX;
            ratioY = (double)currentY / (double)standardResolutionY;

            if (sourceRectangle == Rectangle.Empty)
            {
                game.SpriteBatch.Draw(
                    texture,
                    new Rectangle(
                        (int)(destinationRectangle.X * ratioX),
                        (int)(destinationRectangle.Y * ratioY),
                        (int)(destinationRectangle.Width * ratioX),
                        (int)(destinationRectangle.Height * ratioY)
                    ),
                    color);
            }
            else
            {
                game.SpriteBatch.Draw(
                    texture,
                    new Rectangle(
                        (int)(destinationRectangle.X * ratioX),
                        (int)(destinationRectangle.Y * ratioY),
                        (int)(destinationRectangle.Width * ratioX),
                        (int)(destinationRectangle.Height * ratioY)),
                    sourceRectangle,
                    color
                    );
            }
        }

        internal void Draw(Texture2D texture, Rectangle destinationRectangle, Color color)
        {
            currentX = game.graphics.PreferredBackBufferWidth;
            currentY = game.graphics.PreferredBackBufferHeight;
            ratioX = (double)currentX / (double)standardResolutionX;
            ratioY = (double)currentY / (double)standardResolutionY;
                game.SpriteBatch.Draw(
                    texture,
                    new Rectangle(
                        (int)(destinationRectangle.X * ratioX),
                        (int)(destinationRectangle.Y * ratioY),
                        (int)(destinationRectangle.Width * ratioX),
                        (int)(destinationRectangle.Height * ratioY)),
                    color
                    );
        }

        internal Rectangle ScaleRectangle(Rectangle rect)
        {
            currentX = game.graphics.PreferredBackBufferWidth;
            currentY = game.graphics.PreferredBackBufferHeight;
            ratioX = (double)currentX / (double)standardResolutionX;
            ratioY = (double)currentY / (double)standardResolutionY;
            return new Rectangle(
                    (int)(rect.X * ratioX),
                    (int)(rect.Y * ratioY),
                    (int)(rect.Width * ratioX),
                    (int)(rect.Height * ratioY)
                );
        }

        internal Vector2 ScaleToVector(Vector2 rect)
        {
            currentX = game.graphics.PreferredBackBufferWidth;
            currentY = game.graphics.PreferredBackBufferHeight;
            ratioX = (double)currentX / (double)standardResolutionX;
            ratioY = (double)currentY / (double)standardResolutionY;
            return new Vector2(
                    (int)(rect.X * ratioX),
                    (int)(rect.Y * ratioY)
                );
        }

        internal float ScaleRatio()
        {
            currentX = game.graphics.PreferredBackBufferWidth;
            currentY = game.graphics.PreferredBackBufferHeight;
            ratioX = (double)currentX / (double)standardResolutionX;
            return (float)ratioX;
        }

        internal void ChangeResolutionTo(int X, int Y)
        {
            game.graphics.PreferredBackBufferWidth = X;
            game.graphics.PreferredBackBufferHeight = Y;
            game.graphics.ApplyChanges();
            ScaleGameField(X, Y);
        }

        internal void ScaleGameField(int X, int Y)
        {
            if(Y!=768)
                MovementBoundaries.gameField = new Rectangle(0, 125 + (int)(((Y - 125) / 2) * ScaleRatio()), X, Y - 125 - (int)(((Y - 125) / 2) * ScaleRatio()));
            else
                MovementBoundaries.gameField = new Rectangle(0, 125 + (int)(275 * ScaleRatio()), X, Y - 125 - (int)(275 * ScaleRatio()));
            if (Y == 480)
            {
                MovementBoundaries.gameField.Y += 10;
                MovementBoundaries.gameField.Height -= 10;
            }
        }

        internal void SwitchToFullScreen()
        {
           game.graphics.IsFullScreen = true;
           game.graphics.ApplyChanges();
        }

        internal void SwitchToWindowMode()
        {
           game.graphics.IsFullScreen = false;
           game.graphics.ApplyChanges();
        }
    }
}
